#include "main.h"
#include "i2c.h"
#include "dcmi.h"
#include "dma.h"
#include "tim.h"
#include "gpio.h"
#include "OV2640.h"


	
void OV2640_CLK_ON(void){
	HAL_TIM_Base_Start(&htim12);
	HAL_TIM_PWM_Start(&htim12,TIM_CHANNEL_1);
}
void OV2640_CLK_OFF(void){
	
	HAL_TIM_PWM_Stop(&htim12,TIM_CHANNEL_1);	
}

void OV2640_RST(void){
	HAL_GPIO_WritePin(DCMI_POWD_GPIO_Port,DCMI_POWD_Pin,0);
	HAL_GPIO_WritePin(DCMI_RST_GPIO_Port,DCMI_RST_Pin,1);
	HAL_Delay(50);
	HAL_GPIO_WritePin(DCMI_RST_GPIO_Port,DCMI_RST_Pin,0);
	HAL_Delay(50);
	HAL_GPIO_WritePin(DCMI_RST_GPIO_Port,DCMI_RST_Pin,1);
	HAL_Delay(500);
}

uint8_t SCCB_wr(uint8_t regID,uint8_t regDat){
	uint8_t res=0;
	res=HAL_I2C_Mem_Write(&hi2c2,OV2640ID,regID,I2C_MEMADD_SIZE_8BIT,&regDat,1,100);
	return res;
}

uint8_t OV2640_RDreg(uint8_t regID, uint8_t *regDat){
	uint8_t res=0;
	res=HAL_I2C_Master_Transmit(&hi2c2,OV2640ID,&regID,1,100);
	res=HAL_I2C_Master_Receive(&hi2c2,OV2640ID,regDat,1,100);
//	res=HAL_I2C_Mem_Read(&hi2c1,OV2640ID,regID,I2C_MEMADD_SIZE_8BIT,regDat,1,100);
	return res;
}






void set_OV2640reg(void){
	SCCB_wr(0xff, 0x01);//
	SCCB_wr(0x12, 0x80);
	HAL_Delay(5);
	
	
	SCCB_wr(0xff, 0x00);//

	SCCB_wr(0x2c, 0xff);
	SCCB_wr(0x2e, 0xdf);
	
	
	SCCB_wr(0xff, 0x01);//
	SCCB_wr(0x3c, 0x32);
	//
	SCCB_wr(0x11, 0x00);
	SCCB_wr(0x09, 0x02);
	SCCB_wr(0x04, 0x28);
	SCCB_wr(0x13, 0xe5);
	SCCB_wr(0x14, 0x48);

	SCCB_wr(0x2c, 0x0c);
	SCCB_wr(0x33, 0x78);
	SCCB_wr(0x3a, 0x33);
	SCCB_wr(0x3b, 0xfB);
	//
	SCCB_wr(0x3e, 0x00);
	SCCB_wr(0x43, 0x11);
	SCCB_wr(0x16, 0x10);
	//
	SCCB_wr(0x39, 0x92);
	//
	SCCB_wr(0x35, 0xda);
	SCCB_wr(0x22, 0x1a);
	SCCB_wr(0x37, 0xc3);
	SCCB_wr(0x23, 0x00);
	SCCB_wr(0x34, 0xc0);
	SCCB_wr(0x36, 0x1a);
	SCCB_wr(0x06, 0x88);
	SCCB_wr(0x07, 0xc0);
	SCCB_wr(0x0d, 0x87);
	SCCB_wr(0x0e, 0x41);
	SCCB_wr(0x4c, 0x00);
	SCCB_wr(0x48, 0x00);
	SCCB_wr(0x5B, 0x00);
	SCCB_wr(0x42, 0x03);
	//
	SCCB_wr(0x4a, 0x81);
	SCCB_wr(0x21, 0x99);
	//
	SCCB_wr(0x24, 0x40);
	SCCB_wr(0x25, 0x38);
	SCCB_wr(0x26, 0x82);
	SCCB_wr(0x5c, 0x00);
	SCCB_wr(0x63, 0x00);
	SCCB_wr(0x46, 0x22);
	SCCB_wr(0x0c, 0x3c);
	//
	SCCB_wr(0x61, 0x70);
	SCCB_wr(0x62, 0x80);
	SCCB_wr(0x7c, 0x05);
	//
	SCCB_wr(0x20, 0x80);
	SCCB_wr(0x28, 0x30);
	SCCB_wr(0x6c, 0x00);
	SCCB_wr(0x6d, 0x80);
	SCCB_wr(0x6e, 0x00);
	SCCB_wr(0x70, 0x02);
	SCCB_wr(0x71, 0x94);

	SCCB_wr(0x73, 0xc1);
	//
	SCCB_wr(0x12, 0x40);
	SCCB_wr(0x17, 0x11);
	SCCB_wr(0x18, 0x43);
	SCCB_wr(0x19, 0x00);
	SCCB_wr(0x1a, 0x4b);
	SCCB_wr(0x32, 0x09);
	SCCB_wr(0x37, 0xc0);
	SCCB_wr(0x4f, 0xca);
	SCCB_wr(0x50, 0xa8);
	SCCB_wr(0x5a, 0x23);
	SCCB_wr(0x6d, 0x00);
	SCCB_wr(0x3d, 0x38);
	//
	SCCB_wr(0xff, 0x00);//
	SCCB_wr(0xe5, 0x7f);
	SCCB_wr(0xf9, 0xc0);
	SCCB_wr(0x41, 0x24);
	SCCB_wr(0xe0, 0x14);
	SCCB_wr(0x76, 0xff);
	SCCB_wr(0x33, 0xa0);
	SCCB_wr(0x42, 0x20);
	SCCB_wr(0x43, 0x18);
	SCCB_wr(0x4c, 0x00);
	SCCB_wr(0x87, 0xd5);
	SCCB_wr(0x88, 0x3f);
	SCCB_wr(0xd7, 0x03);
	SCCB_wr(0xd9, 0x10);
	SCCB_wr(0xd3, 0x82);
	//
	SCCB_wr(0xc8, 0x08);
	SCCB_wr(0xc9, 0x80);
	//
	SCCB_wr(0x7c, 0x00);
	SCCB_wr(0x7d, 0x00);
	SCCB_wr(0x7c, 0x03);
	SCCB_wr(0x7d, 0x48);
	SCCB_wr(0x7d, 0x48);
	SCCB_wr(0x7c, 0x08);
	SCCB_wr(0x7d, 0x20);
	SCCB_wr(0x7d, 0x10);
	SCCB_wr(0x7d, 0x0e);
	//
	SCCB_wr(0x90, 0x00);
	SCCB_wr(0x91, 0x0e);
	SCCB_wr(0x91, 0x1a);
	SCCB_wr(0x91, 0x31);

	SCCB_wr(0x91, 0x5a);
	SCCB_wr(0x91, 0x69);
	SCCB_wr(0x91, 0x75);
	SCCB_wr(0x91, 0x7e);
	SCCB_wr(0x91, 0x88);
	SCCB_wr(0x91, 0x8f);
	SCCB_wr(0x91, 0x96);
	SCCB_wr(0x91, 0xa3);
	SCCB_wr(0x91, 0xaf);
	SCCB_wr(0x91, 0xc4);
	SCCB_wr(0x91, 0xd7);
	SCCB_wr(0x91, 0xe8);
	SCCB_wr(0x91, 0x20);
	//
	SCCB_wr(0x92, 0x00);
	SCCB_wr(0x93, 0x06);
	SCCB_wr(0x93, 0xe3);
	SCCB_wr(0x93, 0x05);
	SCCB_wr(0x93, 0x05);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x04);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x00);
	SCCB_wr(0x93, 0x00);
	//
	SCCB_wr(0x96, 0x00);
	SCCB_wr(0x97, 0x08);
	SCCB_wr(0x97, 0x19);
	SCCB_wr(0x97, 0x02);
	SCCB_wr(0x97, 0x0c);
	SCCB_wr(0x97, 0x24);
	SCCB_wr(0x97, 0x30);
	SCCB_wr(0x97, 0x28);
	SCCB_wr(0x97, 0x26);
	SCCB_wr(0x97, 0x02);
	SCCB_wr(0x97, 0x98);
	SCCB_wr(0x97, 0x80);
	SCCB_wr(0x97, 0x00);
	SCCB_wr(0x97, 0x00);
	//
	SCCB_wr(0xc3, 0xed);
	SCCB_wr(0xa4, 0x00);
	SCCB_wr(0xa8, 0x00);
	SCCB_wr(0xc5, 0x11);

	SCCB_wr(0xc6, 0x51);
	SCCB_wr(0xbf, 0x80);
	SCCB_wr(0xc7, 0x10);
	SCCB_wr(0xb6, 0x66);
	SCCB_wr(0xb8, 0xA5);
	SCCB_wr(0xb7, 0x64);
	SCCB_wr(0xb9, 0x7C);
	SCCB_wr(0xb3, 0xaf);
	SCCB_wr(0xb4, 0x97);
	SCCB_wr(0xb5, 0xFF);
	SCCB_wr(0xb0, 0xC5);
	SCCB_wr(0xb1, 0x94);
	SCCB_wr(0xb2, 0x0f);
	SCCB_wr(0xc4, 0x5c);
	//
	SCCB_wr(0xc0, 0x50);    	//imgx
	SCCB_wr(0xc1, 0x3C);     //imgy
	SCCB_wr(0x8c, 0x00);
	SCCB_wr(0x86, 0x3D);
	SCCB_wr(0x50, 0x00);
	SCCB_wr(0x51, 0x50);  	//X
	SCCB_wr(0x52, 0x3C);		//Y	
	SCCB_wr(0x53, 0x00);
	SCCB_wr(0x54, 0x00);
	SCCB_wr(0x55, 0x00);		
	SCCB_wr(0x5a, 0x50);		//oX
	SCCB_wr(0x5b, 0x3C);		//oY
	SCCB_wr(0x5c, 0x00);
	SCCB_wr(0xd3, 0x82);
	//
	SCCB_wr(0xc3, 0xed);
	SCCB_wr(0x7f, 0x00);
	//
	SCCB_wr(0xda, 0x09);
	//
	SCCB_wr(0xe5, 0x1f);
	SCCB_wr(0xe1, 0x67);
	SCCB_wr(0xe0, 0x00);
	SCCB_wr(0xdd, 0x7f);
	SCCB_wr(0x05, 0x00);
}





uint8_t OV2640_init(void)
{
	uint8_t temp;	
	OV2640_CLK_ON();
	OV2640_RST();
	

  //CLK_init_ON();
	temp=0x80;
	if(0!=SCCB_wr(0x12, temp)) //Reset SCCB
	{
    return 1 ;
	}
	HAL_Delay(100);
	set_OV2640reg();

	return 0; //ok			  
} 